#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_addTopLevelItem_clicked()
{
    const QString& s = ui->lineEdit->text();
    int index = ui->treeWidget->topLevelItemCount();
    //int col = ui->treeWidget->topLevelItemCount();
    QTreeWidgetItem* add = new QTreeWidgetItem();
    add->setText(0,s);
    ui->treeWidget->insertTopLevelItem(index,add);
}

void Widget::on_pushButton_addChild_clicked()
{
    const QString& s = ui->lineEdit_2->text();
    QTreeWidgetItem* parent = ui->treeWidget->currentItem();
        QTreeWidgetItem* add = new QTreeWidgetItem();
        int col = ui->treeWidget->currentColumn();
        add->setText(col,s);
        parent->addChild(add);
}

void Widget::on_pushButton_addChild_delete_clicked()
{
    QTreeWidgetItem* current = ui->treeWidget->currentItem();
    if(current==nullptr)
            return;
    else
    {
        QTreeWidgetItem* parent = current->parent();
        if(parent==nullptr)
        {
            int index = ui->treeWidget->indexOfTopLevelItem(current);
            ui->treeWidget->takeTopLevelItem(index);
        }
        else
        {
            parent->removeChild(current);
        }
    }
}
